package com.kenyi.co.ui.me.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.util.Util;
import com.kenyi.co.GlideApp;
import com.kenyi.co.R;
import com.kenyi.co.ui.me.bean.CollectBean;
import com.kenyi.co.ui.me.ui.CollectActivity;
import com.kenyi.co.ui.video.VideoDetailActivity;
import com.kenyi.co.utils.ListBaseAdapter;
import com.kenyi.co.utils.SuperViewHolder;
import com.kenyi.co.view.SwipeMenuView;

import static com.kenyi.co.utils.CommonUtils.isFastClick;

/**
 * Created by Administrator on 2018/1/29.
 */

public class CollectAdapter extends ListBaseAdapter<CollectBean.DatasBean.CollectedArticlesBean> {

    public CollectAdapter(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_right_item_delete_1;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int position) {
        final CollectBean.DatasBean.CollectedArticlesBean item = mDataList.get(position);
        final ImageView iv_item_vodeo = holder.getView(R.id.iv_item_vodeo);
        TextView tv_video_right_title = holder.getView(R.id.tv_video_right_title);
        TextView tv_video_right_sub_title = holder.getView(R.id.tv_video_right_sub_title);
        TextView tv_video_right_num = holder.getView(R.id.tv_video_right_num);
        TextView tv_video_right_time = holder.getView(R.id.tv_video_right_time);
        LinearLayout swipe_content = holder.getView(R.id.swipe_content);
        swipe_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isFastClick()) {
                    return;
                }
                Intent metags = new Intent(mContext, VideoDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("id", item.getId() + "");
//                bundle.putString("id", "1907030516");
                metags.putExtras(bundle);
                mContext.startActivity(metags);
            }
        });
        Button btnDelete = holder.getView(R.id.btnDelete);
        tv_video_right_time.setText(item.getVideoTime());
        tv_video_right_title.setText(item.getTitle());
        tv_video_right_sub_title.setText(item.getSeoQA());
        tv_video_right_num.setText("播放次数：" + item.getReadCount() + "次");

        try {
            if (Util.isOnMainThread() && mContext != null) {
                GlideApp.with(mContext)
                        .load(item.getVideoImg())
                        .placeholder(R.drawable.iv_bg_video_right)
                        .error(R.drawable.iv_bg_video_right)
                        .into(iv_item_vodeo);
            }

        } catch (Exception e) {

        }
        ((SwipeMenuView) holder.itemView).setIos(false).setLeftSwipe(true);
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnSwipeListener) {
                    //如果删除时，不使用mAdapter.notifyItemRemoved(pos)，则删除没有动画效果，
                    //且如果想让侧滑菜单同时关闭，需要同时调用 ((CstSwipeDelMenu) holder.itemView).quickClose();
                    //((CstSwipeDelMenu) holder.itemView).quickClose();
                    mOnSwipeListener.onDel(position);
                }
            }
        });
    }

    /**
     * 和Activity通信的接口
     */
    public interface onSwipeListener {
        void onDel(int pos);

    }

    private onSwipeListener mOnSwipeListener;

    public void setOnDelListener(onSwipeListener mOnDelListener) {
        this.mOnSwipeListener = mOnDelListener;
    }

}
